package cn.tedu.onlinestore.user.service.impl;

import cn.tedu.onlinestore.base.exception.ServiceException;
import cn.tedu.onlinestore.base.response.StatusCode;
import cn.tedu.onlinestore.seller.pojo.entity.Seller;
import cn.tedu.onlinestore.seller.service.SellerService;
import cn.tedu.onlinestore.user.mapper.UserMapper;
import cn.tedu.onlinestore.user.pojo.dto.UserLoginQuery;
import cn.tedu.onlinestore.user.pojo.dto.UserRegisterParam;
import cn.tedu.onlinestore.user.pojo.dto.UserUpdateQuery;
import cn.tedu.onlinestore.user.pojo.entity.User;
import cn.tedu.onlinestore.user.pojo.vo.UserVO;
import cn.tedu.onlinestore.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    SellerService sellerService;
    @Override
    public UserVO select(UserLoginQuery userLoginQuery) {
        log.debug("用户登录业务:{}",userLoginQuery);
        UserVO userVO = userMapper.selectByUsername(userLoginQuery.getUsername());
        if (userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginQuery.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        log.debug("用户查询结果:{}",userVO);
        return userVO;
    }

    @Override
    public void register(UserRegisterParam userRegisterParam) {
        // 检查用户名是否已存在
        String username = userRegisterParam.getUsername();
        UserVO existingUser = userMapper.selectByUsername(username);
        if (existingUser != null) {
            // 如果用户名已存在，抛出自定义异常或返回错误提示
            throw new RuntimeException("用户名已存在，请更换其他用户名！");
        }

        User user = new User();
        BeanUtils.copyProperties(userRegisterParam,user);
        user.setStatus("1");
        user.setCreateTime(new Date());
        userMapper.adduser(user);

        if(user.getLevel().equals("20")){
            Seller seller = new Seller();
            seller.setUsername(user.getUsername());
            seller.setPassword(user.getPassword());
            seller.setLevel(user.getLevel());
            seller.setStatus(user.getStatus());
            seller.setCreateTime(user.getCreateTime());
            sellerService.addSeller(seller);
        }

    }

    @Override
    public void UserUpdate(UserUpdateQuery userUpdateQuery) {
        log.debug("用户更新业务层：{}",userUpdateQuery);
        User user = new User();
        user.setEmail(userUpdateQuery.getEmail());
        user.setPhoneNumber(userUpdateQuery.getPhoneNumber());
        user.setUsername(userUpdateQuery.getUsername());
        user.setId(userUpdateQuery.getId());
        user.setAvatarUrl(userUpdateQuery.getAvatarUrl());
        user.setUpdateTime(new Date());
        user.setStatus(userUpdateQuery.getStatus());
        userMapper.update(user);
    }

    @Override
    public List<UserVO> getAllUser(Integer page, Integer size) {
        log.debug("查询所有用户业务层");
        return userMapper.getAllUser(page,size);
    }
}
